/***************************************************************************
 *  Filename: communicationhandler.h
 *  Class: CommunicationHandler
 *  
 *  Copyright 2012  Jacco van der Spek
 *  <jaccovdspek@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *  
 ****************************************************************************/

/*
 *
 * This file is part of %ProjectName%.
 *
 *    %ProjectName% is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    %ProjectName% is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with %ProjectName%.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef COMMUNICATIONHANDLER_H
#define COMMUNICATIONHANDLER_H

#include <QObject>
#include <qextserialport.h>
#include <QTimer>

class CommunicationHandler : public QObject
{
    Q_OBJECT
public:
    explicit CommunicationHandler(bool raw, QObject *parent = 0);

    void setupPort(QString portname, BaudRateType baudrate, DataBitsType databits,
                   ParityType parity, FlowType flowctrl, StopBitsType stopbits);
    void sendToPort(QByteArray bytes);
    QString readLine();
    QString receiveFromPort();
    QByteArray receiveRawFromPort(int length=-1);
    void closePort();
    bool checkPort();
signals:
    void newLineReady();
    void byteAvailable();
private:
    void send(int count, char *bytes);
    int receive(int count, char *bytes);
    QextSerialPort *serial;
    QTimer *timer;
    QByteArray buffer;
    bool rawData;

private slots:
    void getByte();
};

#endif // COMMUNICATIONHANDLER_H
